app.controller('shopTechnicianCtrl',['$scope','$state','utilService','toaster','$modal','shopTechnicianService',
    function($scope,$state,utilService,toaster,$modal,shopTechnicianService){

    //查询技师列表
    $scope.shopTechnicianVO={};
    $scope.findPageAll = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'shop/technician',
            params: $scope.shopTechnicianVO
        }).then(function (result) {
        });
    };

    $scope.findPageAll();

    //打开查看服务框
    $scope.check = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/shop/shop_technician_info.html',
            controller: 'shopTechnicianInfoCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };


    //删除该技师
    $scope.del = function(id){
        // query data.
        openModal("确定要删除该技师信息吗？");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"message",
                controller:"deleteCtrl",
                message:msg,
                data:id
            });
        }
    };
}]);

app.controller('shopTechnicianAddCtrl',['$scope','$state','utilService','toaster','$modal','shopTechnicianService',
    function($scope,$state,utilService,toaster,$modal,shopTechnicianService){
    $scope.path=$scope.app.image.technician;
    $scope.frontLimit=1;
    $scope.frontImgQueue=new Array();

    $scope.weeks=[{name:'周一',state:false},{name:'周二',state:false},{name:'周三',state:false},{name:'周四',state:false},
        {name:'周五',state:false},{name:'周六',state:false},{name:'周日',state:false}];
    $scope.times=['0:00','0:30','1:00','1:30','2:00','2:30','3:00','3:30','4:00','4:30','5:00','5:30','6:00','6:30','7:00','7:30','8:00',
        '8:30','9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00',
        '16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30'];

    $scope.shopTechnicianVO={
        gender:0,
        isOpen:0
    };

    $scope.addTechnician = function () {

        if($scope.frontImgQueue.length>0){
            $scope.shopTechnicianVO.picture=$scope.frontImgQueue[0].fileName;
        }else{
            delete $scope.shopTechnicianVO.picture;
        }

        $scope.shopTechnicianVO.workTime=$scope.shopTechnicianVO.workStartTime+'-'+$scope.shopTechnicianVO.workEndTime;
        if($scope.shopTechnicianVO.isOpen==1){
            $scope.shopTechnicianVO.restTime=$scope.shopTechnicianVO.restStartTime+'-'+$scope.shopTechnicianVO.restEndTime;
        }
        $scope.shopTechnicianVO.canReserve=selectWeek();

        shopTechnicianService.addTechnician($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go("app.shopTechnician");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    //修改周期
    var selectWeek=function(){
        var day='';
        for(var i=0;i<$scope.weeks.length;i++){
            if($scope.weeks[i].state){
                day+=$scope.weeks[i].name+',';
            }
        }
        var index=day.lastIndexOf(',');
        if(index>-1){
            day=day.substring(0,index);
        }
        return day;
    };

    //循环遍历日期展示
    var openDay=function(openDays){
        var days=openDays.split(',');
        for(var i=0;i<$scope.weeks.length;i++){
            var index=days.indexOf($scope.weeks[i].name);
            if(index>-1){
                $scope.weeks[i].state=true;
            }
        }
    };

}]);

app.controller('shopTechnicianInfoCtrl',['$scope','toaster','data','$modalInstance','shopTechnicianService',
    function($scope,toaster,data,$modalInstance,shopTechnicianService){
    $scope.id=data;
    $scope.info={};

    $scope.findById=function(){
        shopTechnicianService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.info=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.findById();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);

//删除技师信息模态框
app.controller('deleteCtrl', ['$scope', '$modalInstance','$state','shopTechnicianService','toaster','modalData',
    function($scope, $modalInstance,$state,shopTechnicianService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.technicianId = {
            id: modalData.data
        };
        $scope.isDelModal=true;

        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            shopTechnicianService.del($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.isDelModal=true;
                        $scope.findPageAll();
                        break;
                    case 729:
                        $modalInstance.dismiss('error');
                        toaster.pop('error', "操作失败", "找不到该技师信息！");
                        $scope.isDelModal=true;
                        $scope.findPageAll();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

app.controller('shopTechnicianEditCtrl',['$scope','$state','utilService','toaster','$modal','shopTechnicianService','$stateParams',
    function($scope,$state,utilService,toaster,$modal,shopTechnicianService,$stateParams){
    $scope.path=$scope.app.image.technician;
    $scope.frontImages=new Array();
    $scope.frontImgQueue=new Array();

    $scope.weeks=[{name:'周一',state:false},{name:'周二',state:false},{name:'周三',state:false},{name:'周四',state:false},
        {name:'周五',state:false},{name:'周六',state:false},{name:'周日',state:false}];
    $scope.times=['0:00','0:30','1:00','1:30','2:00','2:30','3:00','3:30','4:00','4:30','5:00','5:30','6:00','6:30','7:00','7:30','8:00',
        '8:30','9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00',
        '16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30'];

    $scope.shopTechnicianVO={
        gender:0,
        id:$stateParams.id
    };


    //查询店铺信息
    $scope.findInfo=function(){
        shopTechnicianService.findInfo($scope).then(function (result) {
            $scope.frontLimit=1;
            switch (result.status){
                case 200:
                    var st=$scope.shopTechnicianVO=result.data;

                    if($scope.shopTechnicianVO.canReserve!=null || angular.isUndefined($scope.shopTechnicianVO.canReserve)){
                        openDay($scope.shopTechnicianVO.canReserve);
                    }

                    if($scope.shopTechnicianVO.restStartTime!=null && $scope.shopTechnicianVO.restEndTime!=null){
                        $scope.shopTechnicianVO.isOpen=1;
                    }else{
                        $scope.shopTechnicianVO.isOpen=0;
                    }

                    if(!angular.isUndefined(st.picture)&& st.picture!=null){
                        if($scope.frontImages.length<=0){
                            $scope.frontImages.push(st.picture);
                        }
                        $scope.frontLimit=0;
                    }else{
                        $scope.frontImages.splice(0,$scope.frontImages.length);
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };
    $scope.findInfo();


    $scope.edit = function () {

        if($scope.frontImgQueue.length>0){
            $scope.shopTechnicianVO.picture=$scope.frontImgQueue[0].fileName;
        }else{
            delete $scope.shopTechnicianVO.picture;
        }

        $scope.shopTechnicianVO.workTime=$scope.shopTechnicianVO.workStartTime+'-'+$scope.shopTechnicianVO.workEndTime;
        if($scope.shopTechnicianVO.isOpen==1){
            $scope.shopTechnicianVO.restTime=$scope.shopTechnicianVO.restStartTime+'-'+$scope.shopTechnicianVO.restEndTime;
        }
        $scope.shopTechnicianVO.canReserve=selectWeek();

        shopTechnicianService.edit($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go("app.shopTechnician");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    //修改周期
    var selectWeek=function(){
        var day='';
        for(var i=0;i<$scope.weeks.length;i++){
            if($scope.weeks[i].state){
                day+=$scope.weeks[i].name+',';
            }
        }
        var index=day.lastIndexOf(',');
        if(index>-1){
            day=day.substring(0,index);
        }
        return day;
    };

    //循环遍历日期展示
    var openDay=function(openDays){
        var days=openDays.split(',');
        for(var i=0;i<$scope.weeks.length;i++){
            var index=days.indexOf($scope.weeks[i].name);
            if(index>-1){
                $scope.weeks[i].state=true;
            }
        }
    };


    //删除图片
    $scope.delFrontImage= function (url) {
        shopTechnicianService.delFrontImage($scope,url).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findInfo();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

}]);